package com.leo.train.member.controller;

import com.leo.train.common.resp.CommonResp;
import com.leo.train.common.resp.PageResult;
import com.leo.train.member.domain.Passenger;
import com.leo.train.member.dto.PassengerQueryDto;
import com.leo.train.member.service.PassengerService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/passenger")
@Slf4j
public class PassengerController {
    @Autowired
    private PassengerService passengerService;
    @PostMapping("/save")
    public CommonResp<Passenger> save(@RequestBody @Valid Passenger passenger) {
        log.info("新增乘客：{}", passenger);
        passengerService.save(passenger);
        return new CommonResp<>(passenger);
    }
    /**
     * 查询列表
     */
    @GetMapping("/list")
    public CommonResp<PageResult> list(@Valid PassengerQueryDto queryDto){
        log.info("查询列表：{}", queryDto);
        PageResult pageResult = passengerService.list(queryDto);
        return new CommonResp<>(pageResult);
    }

    /**
     * 查询会员详情
     * @param id
     * @ return
     */
    @GetMapping("/detail")
    public CommonResp<Passenger> detail(@RequestParam Long id){
        log.info("查询详情：{}", id);
        Passenger passenger = passengerService.detail(id);
        return new CommonResp<>(passenger);
    }

    /**
     * 删除
     * @param payload
     * @return
     */
    @PostMapping("/delete")
    public CommonResp delete(@RequestBody Map<String, String> payload){
        log.info("删除：{}", payload.get("id"));
        Long idLong = Long.valueOf(payload.get("id"));
        passengerService.delete(idLong);
        return new CommonResp<>();
    }

    /**
     * 查询所有会员
     */
    @GetMapping("/query-mine")
    public CommonResp<List<Passenger>> queryMine() {
        List<Passenger> list = passengerService.queryMine();
        return new CommonResp<>(list);
    }
}
